package com.jacklin.controller;

import com.jacklin.config.ProjectUrlConfig;
import com.jacklin.constant.CookieConstant;
import com.jacklin.constant.RedisContant;
import com.jacklin.entity.SellerInfo;
import com.jacklin.enums.ResultEnum;
import com.jacklin.service.SellerService;
import com.jacklin.utils.CookieUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * 卖家用户
 * @Author 林必昭
 * @Date 2019/4/12 23:32
 */

@Slf4j
@Controller
@RequestMapping("/seller")
public class SellerUserController {

    @Autowired
    private SellerService sellerService;

    @Autowired private StringRedisTemplate redisTemplate;

    @Autowired
    private ProjectUrlConfig projectUrlConfig;


    @GetMapping("/login")
    public ModelAndView login(@RequestParam("openid") String openid, HttpServletResponse response, Map<String, Object> map){
        //1.openid跟数据库的数据匹配
        SellerInfo sellerInfo = sellerService.findSellerInfoByOpenid(openid);

        if (sellerInfo==null){
            map.put("msg", ResultEnum.LOGIN_FAIL.getMessage());
            map.put("url", "/newstarsell/order/list");
            return new ModelAndView("commom/error");    //登陆失败跳转到error页面
        }

        //2.设置token至redis
        String token = UUID.randomUUID().toString();
        Integer expire = RedisContant.EXPIRE;
        redisTemplate.opsForValue().set(String.format(RedisContant.TOKEN_PRIFIX,token),openid,expire, TimeUnit.SECONDS);//参数1：redis的key，参数2：value，参数3：过期时间，参数4：时间单位.

        //3.设置token到cookie
        CookieUtil.set(response, CookieConstant.TOKEN,token,expire);
        return new ModelAndView("redirect:"+projectUrlConfig.newstarsell+"/newstarsell/seller/order/list"); //获取完后跳转的地址

    }

    @GetMapping("/logout")
    public ModelAndView logout(HttpServletRequest request,HttpServletResponse response,Map<String,Object> map){

        //从cookie查询
        Cookie cookie = CookieUtil.get(request, CookieConstant.TOKEN);

        if (cookie!=null) {
            //2.清除redis
            redisTemplate.opsForValue().getOperations().delete(String.format(RedisContant.TOKEN_PRIFIX, cookie.getValue()));
            //3.清除cookie
            CookieUtil.set(response,CookieConstant.TOKEN,null,0);
        }
        map.put("msg",ResultEnum.LOGOUT_SUCCESS.getMessage());
        map.put("url","/newstarsell/seller/order/list");
        return new ModelAndView("common/success",map);

    }
}
